package com.atguigu.vodtest;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadVideoRequest;
import com.aliyun.vod.upload.resp.UploadVideoResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.*;

import java.util.List;

/**
 * @author He Wentao
 * @since 2022/9/5 22:53
 */

public class InitObject {

    //填入AccessKey信息
    private static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai";  // 点播服务接入地域，固定的，不需要更改
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        return new DefaultAcsClient(profile);
    }

    // 获取播放地址函数
    private static GetPlayInfoResponse getPlayInfo(DefaultAcsClient client,String videoID) throws Exception {
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(videoID);
        return client.getAcsResponse(request);
    }



    /**
     *  根据 accessKey 获取普通视频播放地址
     * @param accessKeyId accessKeyId
     * @param accessKeySecret accessKeySecret
     * @param videoID 视频 id
     */
    public static void test1(String accessKeyId, String accessKeySecret,String videoID){
        // 创建要使用的对象
        DefaultAcsClient client;
        GetPlayInfoResponse response = new GetPlayInfoResponse();
        try {
            // 使用 AccessKey 获取到链接对象
            client = initVodClient(accessKeyId, accessKeySecret);

            // 根据视频 id ，获取到播放视频地址
            response = getPlayInfo(client, videoID);

            // 获取到返回的相关视频信息
            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();

            // 获取地址
            for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
                System.out.print("PlayInfo.PlayURL = " + playInfo.getPlayURL() + "\n");
            }
            // 获取视频名称
            System.out.print("VideoBase.Title = " + response.getVideoBase().getTitle() + "\n");

        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
        // 视频 id
        System.out.print("RequestId = " + response.getRequestId() + "\n");
    }

    /**
     *  根据 accessKey 获取到视频播放凭证
     * @param accessKeyId  accessKey
     * @param accessKeySecret accessKeySecret
     * @param videoID 视频 id
     * @throws Exception 异常
     */
    public static void test2(String accessKeyId, String accessKeySecret,String videoID) throws Exception{
        // 获取初始化对象
        DefaultAcsClient client = InitObject.initVodClient(accessKeyId, accessKeySecret);
        // 创建获取视频凭证 Request 和 response
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();

        // 向 Request 设置视频 id
        request.setVideoId(videoID);

        // 调用初始化方法，获取到视频播放凭证
        response = client.getAcsResponse(request);

        // 输出获取到的凭证
        System.out.println("凭证："+response.getPlayAuth());

    }


    /**
     * 本地文件上传接口
     *
     * @param accessKeyId  accessKeyId
     * @param accessKeySecret  accessKeySecret
     * @param title  视频标题
     * @param fileName   要上传文件的路径
     */
    public static void testUploadVideo(String accessKeyId, String accessKeySecret, String title, String fileName) {
        UploadVideoRequest request = new UploadVideoRequest(accessKeyId, accessKeySecret, title, fileName);
        /* 可指定分片上传时每个分片的大小，默认为2M字节 */
        request.setPartSize(2 * 1024 * 1024L);
        /* 可指定分片上传时的并发线程数，默认为1，(注：该配置会占用服务器CPU资源，需根据服务器情况指定）*/
        request.setTaskNum(1);
        // 开始上传视频
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadVideoResponse response = uploader.uploadVideo(request);
        // 请求视频点播服务的请求ID
        System.out.print("RequestId=" + response.getRequestId() + "\n");

        // 判断调用回调 url 是否成功
        if (response.isSuccess()) {
            System.out.print("VideoId=" + response.getVideoId() + "\n");
        } else {
            /*
                如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。
                其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
            */
            System.out.print("VideoId=" + response.getVideoId() + "\n");
            System.out.print("ErrorCode=" + response.getCode() + "\n");
            System.out.print("ErrorMessage=" + response.getMessage() + "\n");
        }
    }

}
